<?php

class IndexAction extends Action {
    public function index(){
	}
	
	public function bookmanage(){
	    $Data = M('Librarybooks'); // 实例化Data数据模型
        $this->data = $Data->order("Book_ID DESC")->select();
        $this->display();
    }
	//添加图书
	public function bookadd(){
	    
		$Librarybooks = D('Librarybooks');
		if($Librarybooks->create()){
			$Result=$Librarybooks->add();
			if($Result){
				$this->success('操作成功！');
			}else{
				$this->error('操作失败！');
			}
		}else{
		   $this->error($Librarybooks->getError());
		}
	}
    //删除图书
	public function bookdel($id){
	 $Data = M('Librarybooks');
	 $result=$Data->delete($id);
	 echo $result;
	}
	
	public function bookborrow(){
		session(null);//删除所有session。
		$this->display();
	}
	//会员刷卡
	public function getstuinfo($stucard){
		session(null);//先清除session，以免有上个会员的残留信息
		//搜索会员信息
		$data=M("stu_info");
		$condition1["Stu_Card"]=$stucard;
		$stuinfo= $data -> where($condition1)->limit(1)->select();
		if($stuinfo){
			//每个会员手中最多有两本书，检查会员是否符合借书条件。
			$canborrow=M("Libraryrecord");
			$condition2["Stu_ID"]=$stuinfo[0]['Stu_ID'];
			$condition2["Return_Date"]=array('exp','is NULL');
			$bookcount=$canborrow->where($condition2)->count();
			$booknumber= C('BOOK_NUMBER_BORROW');
			if($bookcount<$booknumber){
			  session('stuid',$stuinfo[0]['Stu_ID']);
			  session('stuname',$stuinfo[0]['Stu_Name']);
			  session('leftnumber',$booknumber-$bookcount);
			  echo session('stuname').'，欢迎借书！您已借阅'.$bookcount.'本书，还可以借阅'.session('leftnumber').'本书';	
			}else{
			  echo "每人最多只能借 ".$booknumber." 本书籍，请归还后再借阅！";  
			}
			
			
		}else{
			echo("卡号有误，请重刷！"); 
		}
	}
	//书籍信息
	public function getbookinfo($bookisbn){
	  if(session("?stuid")&&session('leftnumber')){
		  $data=M("Librarybooks");
		  $condition1["Book_Isbn"]=$bookisbn;
		  $condition1["Book_Number"]=array("neq",0);
		  $bookinfo=$data->where($condition1)->limit(1)->select();
		  if($bookinfo){
			 if(session('cartnumber')<session('leftnumber')){
				session('cartnumber',session('cartnumber')+1);
				//借书记录写入数据表
				$Libraryrecord=D("Libraryrecord");
				$Libraryrecord->Book_ID=$bookinfo[0]["Book_ID"];
				$Libraryrecord->Stu_ID=session("stuid");
				$Libraryrecord->Borrow_Date=time();
				$Libraryrecord->add();
				
				//书籍数量减1
				$Librarybooks=M("Librarybooks");
				$Librarybooks->where("Book_Isbn=".$bookisbn)->setDec("Book_Number");

				echo "图书：".$bookinfo[0]['Book_Name']." 借阅成功";
				if(session("cartnumber")>=session('leftnumber')){
					session(null);
					echo "</br>达到最大借书数量，借书结束";
				}
			  }else{
				session(null);
				echo "已达到最大借阅数量。请先还书后再借阅。";
			  } 
		  }else{
			  echo("</br>1.请重刷条码！检查条码是否出错。</br> 2.如条码正确，此书尚未入库。或者借出未归还。请把此书收回交给IT处理！！！");
		  }
	  }else{
		  session(null);
		  echo "无权借书！未刷会员卡，或者借书超出数量。";
	  }
	}


}

?>